package cn.cttic.app.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.cttic.app.service.IAppSmsService;
import cn.cttic.common.core.constant.CacheConstants;
import cn.cttic.common.redis.utils.RedisUtils;
import cn.cttic.resource.api.RemoteSmsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @Implementation: 微信小程序短信接口
 * @Author: Times.Studio
 * @Date: 2024.05.15
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AppSmsServiceImpl implements IAppSmsService {

    @DubboReference
    private final RemoteSmsService remoteSmsService;

    /**
     * 发送短信验证码
     *
     * @param phone   手机号
     * @param smsCode 短信验证码
     * @return
     */
    @Override
    public Boolean sendSmsCode(String phone, String smsCode) {
        return null;
    }

    /**
     * 发送短信验证码[模拟]
     *
     * @param phone   手机号
     * @param smsCode 短信验证码
     * @return
     */
    @Override
    public String sendFakeSmsCode(String phone) {
        String smsCode = RandomUtil.randomNumbers(6);
        RedisUtils.setCacheObject(CacheConstants.CAPTCHA_CODE_KEY + phone, smsCode);
        return smsCode;
    }

    /**
     * 验证短信验证码
     *
     * @param phone   手机号
     * @param smsCode 短信验证码
     * @return
     */
    @Override
    public Boolean validateSmsCode(String phone, String smsCode) {
        String code = RedisUtils.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + phone);
        return code.equals(smsCode);
    }
}
